package com.zoshine.zzg.service;

import com.github.pagehelper.util.StringUtil;
import com.zoshine.zzg.dao.UserDao;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.User;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * 用户服务
 *
 * @author 田沛凌
 * @time 2017年6月29日 下午8:14:03
 */
@Service
@Transactional
public class UserService extends BaseService<User> {
    private UserDao userDao;
    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 用户登录验证
     * @param username 用户名
     * @param password 密码
     */
    public User loginCheck(String username, String password) throws NoSuchAlgorithmException {
        User user = userDao.selectByUsername(username);
        // 验证用户名
        if (user == null) {
            throw new AppException(ErrorMessage.USERNAME_NOT_EXIST);
        }
        // 验证密码
        password = DigestUtils.md5DigestAsHex(password.getBytes()) + user.getSalt();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!Objects.equals(password, user.getPassword())) {
            throw new AppException(ErrorMessage.PASSWORD_MISTAKE);
        }
        return user;
    }
//	private IdWorker<Long> userIdWorker;
//	private OrganizationDao organizationDao;
//	private RoleDao roleDao;
//	@Autowired
//	public void setUserIdWorker(IdWorker<Long> userIdWorker) {
//		this.userIdWorker = userIdWorker;
//	}
//	@Autowired
//	public void setOrganizationDao(OrganizationDao organizationDao) {
//        this.organizationDao = organizationDao;
//    }
//	@Autowired
//    public void setRoleDao(RoleDao roleDao) {
//        this.roleDao = roleDao;
//    }
//    @Override
//    public User login(String userName, String password) {
//		if(StringUtil.isEmpty(userName) || StringUtil.isEmpty(password)){
//			throw new AppException(ErrorMessage.LOGINNAME_AND_PASSWORD_NOT_NULL);
//		}
//		User user = userDao.selectUserByLogin(userName.trim(), password.trim());
//		if(user == null){
//			throw new AppException(ErrorMessage.LOGINNAME_AND_PASSWORD_NOT_EXIST);
//		}
//        return user;
//    }
//
//	@Override
//	public void delUserById(Long id) {
//		if(id == null){
//			throw new AppException(ErrorMessage.USER_ID_CAN_NOT_NULL);
//		}
//		userDao.delete(id);
//	}
//
	@Override
	public User add(User user) {
		if(StringUtil.isEmpty(user.getUsername())){
			throw new AppException(ErrorMessage.USERNAME_NOT_NULL);
		}
		if(StringUtil.isEmpty(user.getPassword())){
			throw new AppException(ErrorMessage.PASSWORD_NOT_NULL);
		}
		if(null == user.getRoleId()){
			throw new AppException(ErrorMessage.USER_ROLE_ID_NOT_NULL);
		}
		if(null == user.getOrgId()){
			throw new AppException(ErrorMessage.USER_ORG_ID_NOT_NULL);
		}
		if(null == user.getUserStatus()){
			throw new AppException(ErrorMessage.USER_STATUS_NOT_NULL);
		}
		//重名验证
		User repeatUser = userDao.selectByUsername(user.getUsername());
		if(repeatUser != null){
			throw new AppException(ErrorMessage.USER_NAME_ALREADY_OCCUPIED);
		}
		user.setSalt(RandomStringUtils.randomAlphabetic(6));
		String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes()) + user.getSalt();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
		return super.add(user);
	}

//	@Override
//	public User selectUserById(Long id) {
//		return userDao.selectOne(id);
//	}
//
	@Override
	public int edit(User user) {
		if(null == user.getId()){
			throw new AppException(ErrorMessage.USER_ID_CAN_NOT_NULL);
		}
		if(StringUtil.isEmpty(user.getUsername())){
			throw new AppException(ErrorMessage.USER_LOGIN_NAME_CAN_NOT_NULL);
		}
		if(null == user.getOrgId()){
		    throw new AppException(ErrorMessage.USER_ORG_ID_NOT_NULL);
		}
		if(null == user.getRoleId()){
		    throw new AppException(ErrorMessage.USER_ROLE_ID_NOT_NULL);
		}
		return super.edit(user);
	}
//	@Override
//	public List<User> ListUser(User user) {
//		return userDao.selectList(user);
//	}

	/**
	 * 用户密码修改
	 * @param user 用户信息
	 */
	public void editPassword(User user){
    	userDao.updatePassword(user);
	}
}
